package cn.itsource.ymcc.mq;

import cn.itsource.ymcc.constant.MQConstants;
import cn.itsource.ymcc.domain.CourseOrder;
import cn.itsource.ymcc.domain.CourseOrderItem;
import cn.itsource.ymcc.domain.PayOrder;
import cn.itsource.ymcc.service.IPayOrderService;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.stream.Collectors;

@RocketMQMessageListener(
        consumerGroup = MQConstants.TX_COURSER_ORDER_CONSUMER_GROUP,
        topic = MQConstants.TX_COURSER_ORDER_TOPIC,
        selectorExpression = "*",
        messageModel = MessageModel.CLUSTERING //集群，一个订单保存一份支付单

)
@Service
@Slf4j
public class OrderMessageLinster  implements RocketMQListener<MessageExt> {

@Autowired
private IPayOrderService payOrderService;

    @Override
    public void onMessage(MessageExt messageExt) {
        log.info("消费消息，获取order信息");
        byte[] body = messageExt.getBody();
        String josnStr = null;
        try {
            josnStr = new String(body,"utf-8");
            CourseOrder courseOrder = JSON.parseObject(josnStr, CourseOrder.class);
            String orderNo = courseOrder.getOrderNo();
            // 支付单是否过
            synchronized (orderNo){

                Wrapper<PayOrder> wrapper = new EntityWrapper<>();
                wrapper.eq("order_no",orderNo);
                PayOrder payOrder = payOrderService.selectOne(wrapper);
                if(payOrder==null){
                    log.info("保存支付单！");
                    // 保存支付单
                    payOrder = new PayOrder();
                    payOrder.setCreateTime(new Date());
                    payOrder.setCreateTime(new Date());
                    payOrder.setAmount(courseOrder.getTotalAmount());
                    payOrder.setPayType(courseOrder.getPayType());
                    payOrder.setOrderNo(courseOrder.getOrderNo());
                    payOrder.setUserId(courseOrder.getUserId());
                    StringBuffer sb = new StringBuffer();
                    for (CourseOrderItem courseOrderItem : courseOrder.getCourseOrderItems()) {
                        Long courseId = courseOrderItem.getCourseId();
                        sb.append(courseId);
                    }
                    payOrder.setExtParams("userId="+courseOrder.getUserId()+"&relationId="+sb.toString());
                    payOrder.setSubject(courseOrder.getTitle());
                    payOrder.setPayStatus(CourseOrder.NO_PAY); // 未支付！

                    payOrderService.insert(payOrder);
                    log.info("保存支付单完成！！");
                }
            }

        } catch (UnsupportedEncodingException e) {
            log.info("保存支付单失败！！");
            e.printStackTrace();
        }

    }
}
